package com.addplus.townmall.server.api.mapper.town;

import com.addplus.townmall.server.api.model.town.OrderItemsFight;
import com.addplus.townmall.server.api.model.town.Store;
import com.addplus.townmall.server.api.model.town.expand.SolrModelExpand;
import com.addplus.townmall.server.api.model.town.expand.StoreExp;
import com.addplus.townmall.server.api.model.town.expand.StoreProductExpand;
import com.addplus.townmall.server.api.model.town.param.OrderFightParam;
import com.addplus.townmall.server.api.model.town.param.OrderItemsFightParam;
import com.addplus.townmall.server.api.model.town.param.OrderItemsParam;
import com.addplus.townmall.server.api.model.town.param.OrderShopItemsParam;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.annotation.TableField;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * 商城店铺表 Mapper 接口
 * </p>
 *
 * @author zhangjiehang
 * @since 2018-08-27
 */
public interface StoreMapper extends BaseAddMapper<Store> {

    @Select("<script>" +
            "SELECT id as store_id,store_name,icon_path,store_type,freight,free_freight FROM store WHERE id in " +
            "<foreach collection='storeid_list' open='('  item='item'  separator=',' close=')'> #{item}</foreach>" +
            "AND is_delete = 0 AND store_status = 1 " +
            "</script>")
    List<OrderItemsParam> getConfirmOrderStore(@Param("storeid_list")List<Integer> storeIdList);

    @Select("SELECT id as store_id,store_name,icon_path,sender_phone,store_type,sender_address,freight,free_freight FROM store WHERE id= #{storeId} " +
            "AND is_delete = 0 AND store_status = 1")
    OrderItemsParam getConfirmOrderStoreSingle(@Param("storeId")Integer storeId);


    @Select("SELECT id as store_id,store_name,icon_path,store_type,freight,free_freight FROM store WHERE id= #{storeId} " +
            "AND is_delete = 0 AND store_status = 1")
    OrderFightParam getConfirmOrderStoreFight(@Param("storeId") Integer storeId);


    @Select("SELECT s.id as store_id,s.store_name,s.icon_path,s.sender_phone as seller_phone,s.store_type,s.sender_address as seller_address,s.freight,s.free_freight,\n" +
            "p.store_id as shop_store_id,p.id as shop_product_id,p.shop_name,p.shop_seal_img,p.shop_fright_id,p.fright_type,p.is_discount,p.shop_customize_code,\n" +
            "sku.sale_price,sku.cost_price,sku.line_price as shop_price,sku.stock,sku.id as shop_specification_sku_id," +
            "sku.sku_id as shop_specification_sku_sku_id,sku.sku_describe as shop_specification_sku_json,\n" +
            "c.enable_noreason_return as enable_noreason_return " +
            "from shop_product p \n" +
            "LEFT JOIN store s ON p.store_id = s.id AND s.is_delete = 0 AND s.store_status = 1\n" +
            "LEFT JOIN shop_specification_sku sku ON sku.shop_product_id = p.id AND sku.is_deleted = 0 \n" +
            "LEFT JOIN platform_category c ON p.shop_category_id = c.id AND c.is_deleted = 0 \n" +
            "WHERE sku.id = #{sku_id} AND p.id = #{store_id} AND p.audit_status = 0 AND p.is_deleted = 0 AND p.sale_status = 0")
    @Results({
            //映射内嵌实体类
            @Result(column = "shop_product_id",property = "itemsParam.shopProductId"),
            @Result(column = "shop_store_id",property = "itemsParam.storeId"),
            @Result(column = "shop_name",property = "itemsParam.shopName"),
            @Result(column = "shop_seal_img",property = "itemsParam.shopSealImg"),
            @Result(column = "shop_fright_id",property = "itemsParam.shopFrightId"),
            @Result(column = "fright_type",property = "itemsParam.frightType"),
            @Result(column = "is_discount",property = "itemsParam.isDiscount"),
            @Result(column = "shop_customize_code",property = "itemsParam.shopCustomizeCode"),
            @Result(column = "sale_price",property = "itemsParam.salePrice"),
            @Result(column = "cost_price",property = "itemsParam.costPrice"),
            @Result(column = "shop_price",property = "itemsParam.shopPrice"),
            @Result(column = "stock",property = "itemsParam.stock"),
            @Result(column = "shop_specification_sku_id",property = "itemsParam.shopSpecificationSkuId"),
            @Result(column = "shop_specification_sku_sku_id",property = "itemsParam.shopSpecificationSkuSkuId"),
            @Result(column = "shop_specification_sku_json",property = "itemsParam.shopSpecificationSkuJson"),
            @Result(column = "enable_noreason_return",property = "itemsParam.enableNoreasonReturn")
    })
    OrderItemsParam getConfirmOrderStoreSingleOne(@Param("sku_id")Integer skuId,@Param("store_id")Integer storeId);

    @Select("SELECT * FROM store s LEFT JOIN store_detail d on s.id=d.store_id where s.is_delete=0 AND s.id=#{id}")
    StoreExp getStoreDetailById(@Param("id")Integer id);

    @Select("select * from store a where  a.store_type=0 limit 1")
    Store getSelfStore();

    @Select("SELECT\n" +
            "\ts.id,\n" +
            "\ts.store_name,\n" +
            "\ts.store_introduction,\n" +
            "\ts.is_delete,\n" +
            "\ts.gmt_create,\n" +
            "\td.collect_virtual_count,\n" +
            "\td.collect_count,\n" +
            "\td.visit_count,\n" +
            "\td.vistit_virtual_count,\n" +
            "\td.shop_count,\n" +
            "\td.overall_rate,\n" +
            "\td.shop_rate,\n" +
            "\td.service_rate,\n" +
            "\td.logistics_rate\n" +
            "FROM\n" +
            "\tstore s\n" +
            "\tLEFT JOIN store_detail d ON d.store_id = s.id \n" +
            "WHERE\n" +
            "\ts.id = #{id}")
    SolrModelExpand getSolrModel(@Param("id") Integer id);

    @Select("<script>" +
            "SELECT s.id as id,store_name,icon_path,store_introduction,m.nick_name as nickName FROM store s " +
            "LEFT JOIN member m on m.id = s.member_id and m.is_deleted = 0 and is_disable = 0 WHERE s.id in " +
            "<foreach collection='storeid_list' open='('  item='item'  separator=',' close=')'> #{item}</foreach>" +
            "AND s.is_delete = 0 AND s.store_status = 1 " +
            "</script>")
    List<StoreProductExpand> getStoreProduct(@Param("storeid_list")List<Integer> storeIdList);
}
